package com.homework.dao.impl;

import com.homework.common.StringUtils;
import com.homework.dao.UserDao;
import com.homework.model.User;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
* @author HLQ
* @date 2024/9/29 10:10
*/

@Repository
@Slf4j
public class UserDaoImpl  implements UserDao{
    @Autowired
    private Connection conn ;

    @Override
    public int insert(User user) {

        String insertSQL = "INSERT INTO tb_user"+"(user_name,password,salt,email,phone,sex,age)"
                +"VALUES"+"(?,?,?,?,?,?,?)" ;
        try (

                PreparedStatement ps =  conn.prepareStatement(insertSQL) ;
                ){
            //允许为 SQL 语句中的每个参数设置类型
            ps.setString(1,user.getUserName());
            ps.setString(2,StringUtils.md5Encoder(user.getPassword(),user.getSalt())) ;
            ps.setString(3,user.getSalt());
            ps.setString(4, user.getEmail());
            ps.setString(5,user.getPhone());
            ps.setString(6,user.getSex());
            ps.setInt(7,user.getAge());
            return ps.executeUpdate();
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return 0 ;
    }

    @Override
    public void update(User user) {
        String updateSQL = "UPDATE tb_user SET email=?,phone=?,sex=?,age=? WHERE user_name = ?";
        try (

                PreparedStatement ps =  conn.prepareStatement(updateSQL);
        ){
            //允许为 SQL 语句中的每个参数设置类型
            ps.setString(1, user.getEmail());
            ps.setString(2,user.getPhone());
            ps.setString(3,user.getSex());
            ps.setInt(4,user.getAge());
            ps.setString(5,user.getUserName());
            ps.executeUpdate();
        } catch (Exception e) {
            log.error(e.getMessage());
        }
    }

    @Override
    public Integer delete(int userid) {
        String deleteSQL = "DELETE FROM tb_user" + "WHERE user_id = ?";
        try (

                PreparedStatement ps =  conn.prepareStatement(deleteSQL);
        ) {
            //允许为 SQL 语句中的每个参数设置类型
            ps.setInt(1, userid);
            return ps.executeUpdate();

        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return 0;
    }

    @Override
    public User login(String name, String password) {

        String loginSQL = "SELECT * FROM tb_user" +
                "WHERE user_name = ? AND password = ?";
        try (

                PreparedStatement ps =  conn.prepareStatement(loginSQL);
        ){
            ps.setString(1,name);
            ps.setString(2,StringUtils.md5Encoder(password,name));
            ResultSet resultSet = ps.executeQuery();
            if (resultSet.next()){
                User user = new User();
                user.setUserId(Integer.parseInt(String.valueOf(resultSet.getInt("user_id"))));
                user.setUserName(resultSet.getString("user_name"));
                user.setSalt(resultSet.getString("salt"));
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return null ;
    }


    @Override
    public int countAllUsers() {
        String countSQL = "SELECT COUNT(*) AS user_count FROM tb_user";
        try (

                PreparedStatement ps = conn.prepareStatement(countSQL)
        ) {
            ResultSet resultSet = ps.executeQuery();
            if (resultSet.next()){
                return resultSet.getInt("user_count");
            }
        }catch (Exception e){
            log.error(e.getMessage());
        }
        return 0;
    }

}

